import { getAction, deleteAction, putAction, postAction, httpAction, formAction } from '@/api/manage'
import Vue from 'vue'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'

//角色管理
const addRole = (params) => postAction('/sys/role/add', params)
const dealRole2Role = (params) => postAction('/system/rlRole2role/dealRole2Role', params)
const getRelRolesByRoleId = (params) => postAction('/system/rlRole2role/getListByRoleId', params)
const editRole = (params) => putAction('/sys/role/edit', params)
const checkRoleCode = (params) => getAction('/sys/role/checkRoleCode', params)
const queryall = (params) => getAction('/sys/role/queryall', params)
//岗位管理
const getDeptRoleList = (params) => getAction('/sys/sysDepartRole/getDeptRoleList', params)

//用户管理
const dealUserOtherInfo = (params) => getAction('/sys/user/dealUserOtherInfo', params)
const updateAllUserPassword = (params) => putAction('/sys/user/updateAllUserPassword', params)
const addUser = (params) => postAction('/sys/user/add', params)
const editUser = (params) => putAction('/sys/user/edit', params)
const queryUserRole = (params) => getAction('/sys/user/queryUserRole', params)
const getUserList = (params) => getAction('/sys/user/list', params)
const frozenBatch = (params) => putAction('/sys/user/frozenBatch', params)
//验证用户是否存在
const checkOnlyUser = (params) => getAction('/sys/user/checkOnlyUser', params)
//改变密码
const changePassword = (params) => putAction('/sys/user/changePassword', params)
const getSimpleUserList = (params) => getAction('/sys/user/simpleList', params)
const duplicateUserIdcardCheck = (params) => getAction('/sys/user/duplicateUserIdcardCheck', params)

//上传图片
const uploadPhoto = (params) => putAction('/sys/common/upload', params)
//权限管理
const addPermission = (params) => postAction('/sys/permission/add', params)
const editPermission = (params) => putAction('/sys/permission/edit', params)
const deletePermission = (params) => deleteAction('/sys/permission/delete', params)
const getPermissionList = (params) => getAction('/sys/permission/list', params)
const getPermissionByObjId = (params) => getAction('/sys/permission/getByObjId', params)
const getPermissionById = (params) => getAction('/sys/permission/getById', params)
const getPermissionDetailList = (params) => getAction('/sys/permission/formList', params)
const getSystemMenuList = (params) => getAction('/sys/permission/getSystemMenuList', params)
const getSystemSubmenu = (params) => getAction('/sys/permission/getSystemSubmenu', params)

const getSystemSubmenuBatch = (params) => getAction('/sys/permission/getSystemSubmenuBatch', params)
const queryTreeList = (params) => getAction('/sys/permission/queryTreeList', params)
const queryPermissionCats = (params) => getAction('sys/permissioncats/list', params)
const queryTreeListForRole = (params) => getAction('/sys/role/queryTreeList', params)
const queryListAsync = (params) => getAction('/sys/permission/queryListAsync', params)
const queryRolePermission = (params) => getAction('/sys/permission/queryRolePermission', params)
const saveRolePermission = (params) => postAction('/sys/permission/saveRolePermission', params)
const queryPermissionsByUser = () => getAction('/sys/permission/getUserPermissionByToken')
const queryPermissionsByRoleId = (params) => getAction('/sys/permission/getUserPermissionByRoleId', params)
const loadAllRoleIds = (params) => getAction('/sys/permission/loadAllRoleIds', params)
const getPermissionRuleList = (params) => getAction('/sys/permission/getPermRuleListByPermId', params)
const queryPermissionRule = (params) => getAction('/sys/permission/queryPermissionRule', params)
const getSimpleRoleList = (params) => getAction('/sys/role/simpleList', params)

// 部门管理
const getDepartsByDepartPath = (params) => getAction('/sys/sysDepart/getDepartsByDepartPath', params)
const queryDepartTreeList = (params) => getAction('/sys/sysDepart/queryTreeList', params)
const queryDepartTreeSync = (params) => getAction('/sys/sysDepart/queryDepartTreeSync', params)
const queryIdTree = (params) => getAction('/sys/sysDepart/queryIdTree', params)
const queryParentName = (params) => getAction('/sys/sysDepart/queryParentName', params)
const searchByKeywords = (params) => getAction('/sys/sysDepart/searchBy', params)
const deleteByDepartId = (params) => deleteAction('/sys/sysDepart/delete', params)
const getTreeDepartsListByDepartId = (params) => getAction('/sys/sysDepart/getTreeDepartsListByDepartId', params)
const getDepartsByDepartIdsAndRoles = (params) => postAction('/sys/sysDepart/getDepartsByDepartIdsAndRoles', params)
const canSeeOuIds = (params) => getAction('/sys/sysDepart/canSeeOuIds', params)

//二级部门管理
const queryDepartPermission = (params) => getAction('/sys/permission/queryDepartPermission', params)
const saveDepartPermission = (params) => postAction('/sys/permission/saveDepartPermission', params)
const queryTreeListForDeptRole = (params) => getAction('/sys/sysDepartPermission/queryTreeListForDeptRole', params)
const queryDeptRolePermission = (params) => getAction('/sys/sysDepartPermission/queryDeptRolePermission', params)
const saveDeptRolePermission = (params) => postAction('/sys/sysDepartPermission/saveDeptRolePermission', params)
const queryMyDepartTreeList = (params) => getAction('/sys/sysDepart/queryMyDeptTreeList', params)

//日志管理
const deleteLog = (params) => deleteAction('/sys/log/delete', params)
const deleteLogList = (params) => deleteAction('/sys/log/deleteBatch', params)

//数据字典
const addDict = (params) => postAction('/sys/dict/add', params)
const editDict = (params) => putAction('/sys/dict/edit', params)
const treeList = (params) => getAction('/sys/dict/treeList', params)
const addDictItem = (params) => postAction('/sys/dictItem/add', params)
const editDictItem = (params) => putAction('/sys/dictItem/edit', params)
const getDictItemAll = (params) => postAction('/sys/dictItem/listAll', params)

// 字典标签专用（通过code获取字典数组）,若需要获取字典，请优先使用 getDictItemsByCode 方法，该方法会优先从缓存中获取
const ajaxGetDictItems = (code, params) => getAction(`/sys/dict/getDictItems/${code}`, params)
//从缓存中获取字典配置
function getDictItemsFromCache(dictCode) {
  if (Vue.ls.get(UI_CACHE_DB_DICT_DATA) && Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
    let dictItems = Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]
    return dictItems
  }
}

/**
 * 获取字典信息，优先从缓存中获取字典，若缓存中没有，则ajax重新加载字典，并再次放入缓存
 * @param {*} dictCode 字典Code
 * @param {*} ifRefresh 是否刷新缓存
 */
const getDictItemsByCode = (dictCode, ifRefresh = false) => {
  return new Promise(async (resolve) => {
    if (!ifRefresh) {
      const cacheDictItems = getDictItemsFromCache(dictCode)
      if (cacheDictItems) {
        resolve(cacheDictItems)
        return
      }
    }
    let { success, result } = await ajaxGetDictItems(dictCode)
    if (success) {
      resolve(result)
      return
    }
    resolve([])
  })
}

//系统通告
const doReleaseData = (params) => getAction('/sys/annountCement/doReleaseData', params)
const doReovkeData = (params) => getAction('/sys/annountCement/doReovkeData', params)
//获取系统访问量
const getLoginfo = (params) => getAction('/sys/loginfo', params)
const getVisitInfo = (params) => getAction('/sys/visitInfo', params)

// 根据部门主键查询用户信息
const queryUserByDepId = (params) => getAction('/sys/user/queryUserByDepId', params)

// 重复校验
const duplicateCheck = (params) => getAction('/sys/duplicate/check', params)
// 加载分类字典
const loadCategoryData = (params) => getAction('/sys/category/loadAllData', params)
const loadTreeChildren = (params) => getAction('/sys/category/loadTreeChildren', params)
const loadCateTree = (params) => getAction('/sys/category/loadCateTree', params)
const loadDictItem = (params) => getAction('/sys/category/loadDictItem', params)
const checkRuleByCode = (params) => getAction('/sys/checkRule/checkByCode', params)
const getRootList = (params) => getAction('/sys/category/rootList', params)
const getChildList = (params) => getAction('/sys/category/childList', params)
//加载我的通告信息
const getUserNoticeInfo = (params) => getAction('/sys/sysAnnouncementSend/getMyAnnouncementSend', params)
const getTransitURL = (url) => `/sys/common/transitRESTful?url=${encodeURIComponent(url)}`
// 中转HTTP请求
export const transitRESTful = {
  get: (url, parameter) => getAction(getTransitURL(url), parameter),
  post: (url, parameter) => postAction(getTransitURL(url), parameter),
  put: (url, parameter) => putAction(getTransitURL(url), parameter),
  http: (url, parameter) => httpAction(getTransitURL(url), parameter)
}

// 应用管理
const inputListAll = (params) => getAction('/lowcode/fwInputTypes/listAll', params)
const dealForm = (params) => postAction('/lowcode/fwForms/dealForm', params)
const formDetail = (params) => postAction('/lowcode/fwForms/formDetail', params)
const getFormById = (params) => getAction('/lowcode/fwForms/queryById', params)
const getFormList = (params) => getAction('/lowcode/fwForms/list', params)
const editForm = (params) => putAction('/lowcode/fwForms/edit', params)
const downloadForm = (params) => getAction('/lowcode/fwForms/downloadFormTemplate', params)
const getFormTitles = (params) => postAction('/lowcode/fwForms/getFormTitles', params)
const importFormData = (params) => postAction('/lowcode/fwForms/importFormData', params)
const getFileRowNumber = (params) => postAction('/lowcode/fwForms/getFileRowNumber', params)
const exportFormData = (params) => postAction('/lowcode/fwForms/exportFormData', params)
const getFormDataList = (params) => postAction('/lowcode/fwForms/getFormDataList', params)
const getAfTables = (params) => postAction('/sys/permission/getAfTableTree', params)
const getAfTableColmuns = (params) => getAction('/lowcode/fwForms/getAfTableColmuns', params)
const getTableEnumitems = (params) => postAction('/lowcode/fwForms/getTableEnumitems', params)
const subForm = (params) => postAction('/lowcode/fwForms/subForm', params)
const deleteAfDatas = (params) => postAction('/lowcode/fwForms/deleteAfDatas', params)
const addAuth = (params) => postAction('/lowcode/fwAuths/add', params)
const editAuth = (params) => putAction('/lowcode/fwAuths/edit', params)
const deleteAuth = (params) => deleteAction('/lowcode/fwAuths/delete', params)
const listAuth = (params) => getAction('/lowcode/fwAuths/list', params)
const getFormAuthByUsername = (params) => postAction('/lowcode/fwAuths/getMaxAuth', params)
const formListAll = (params) => getAction('/lowcode/fwForms/listAll', params)
const getDyFormOrderId = (params) => postAction('/sg/gvVisitingRec/add', params)

// 应用页面
const addPage = (params) => postAction('/lowcode/fwPage/add', params)
const editPage = (params) => putAction('/lowcode/fwPage/edit', params)
const deletePage = (params) => deleteAction('/lowcode/fwPage/delete', params)
const getPage = (params) => getAction('/lowcode/fwPage/queryById', params)
const listPage = (params) => getAction('/lowcode/fwPage/list', params)
const removePageRelation = (params) => deleteAction('/lowcode/fwPage/removePageRelation', params)

// 系统配置
const addAppParams = (params) => postAction('/system/adAppParam/add', params)
const getAppParams = (params) => getAction('/system/adAppParam/list', params)
const saveAppParams = (params) => putAction('/system/adAppParam/edit', params)
const deleteAppParams = (params) => deleteAction('/system/adAppParam/delete', params)
const listAppParams = (params) => getAction('/system/adAppParam/listAll', params)
const getCdDtlAddr = (params) => postAction('/system/cdDtlAddr/lists', params)
const getCdDtlAddrDetail = (params) => postAction('/system/cdDtlAddr/detail', params)
const getAddressTree = (params) => getAction('/system/cdDtlAddr/getAddressTree', params)

// 工作流:模型接口
const pageBpmModel = (params) => getAction('/bpm/model/page', params)
const getBpmModel = (params) => getAction('/bpm/model/get', params)
const createBpmModel = (params) => postAction('/bpm/model/create', params)
const updateBpmModel = (params) => putAction('/bpm/model/update', params)
const importBpmModel = (params) => postAction('/bpm/model/import', params)
const deployBpmModel = (params) => postAction('/bpm/model/deploy', params)
const updateStateBpmModel = (params) => putAction('/bpm/model/update-state', params)
const deleteBpmModel = (params) => deleteAction('/bpm/model/delete', params)

// 工作流:模型定义接口
const pageBpmProcessDefinition = (params) => getAction('/bpm/process-definition/page', params)
const getBpmProcessDefinition = (params) => getAction('/bpm/process-definition/get', params)
const listBpmProcessDefinition = (params) => getAction('/bpm/process-definition/list', params)
const getBpmProcessDefinitionXml = (params) => getAction('/bpm/process-definition/get-bpmn-xml', params)
const getOutGoingNames = (params) => getAction('/bpm/process-definition/get-outgoing-names', params)
const getNodes = (params) => getAction('/bpm/process-definition/get-nodes', params)
const getLastDefinition = (params) => getAction('/bpm/process-definition/get-last-definition', params)

// 工作流：流转规则
const createTaskAssignRule = (params) => postAction('/bpm/task-assign-rule/create', params)
const getTaskAssignRuleList = (params) => getAction('/bpm/task-assign-rule/list', params)
const updateTaskAssignRule = (params) => putAction('/bpm/task-assign-rule/update', params)

//工作流：用户组
const getUserGroupSimpleList = (params) => getAction('/bpm/user-group/list-all-simple', params)
const createUserGroup = (params) => postAction('/bpm/user-group/create', params)
const updateUserGroup = (params) => putAction('/bpm/user-group/update', params)
const deleteUserGroup = (params) => deleteAction('/bpm/user-group/delete', params)
const getUserGroup = (params) => getAction('/bpm/user-group/get', params)
const pageUserGroup = (params) => getAction('/bpm/user-group/page', params)

// 工作流：流程实例
const pageProcessInstance = (params) => postAction('/bpm/process-instance/my-page', params)
const getEventListByUserId = (params) => postAction('/bpm/process-instance/getEventListByUserId', params)
const pageTodoProcessInstance = (params) => getAction('/bpm/process-instance/todo-page', params)
const pageDoneProcessInstance = (params) => getAction('/bpm/process-instance/done-page', params)
const createProcessInstance = (params) => postAction('/bpm/process-instance/create', params)
const cancelProcessInstance = (params) => httpAction('/bpm/process-instance/cancel', params, 'delete')
const getProcessInstance = (params) => getAction('/bpm/process-instance/get', params)
const switchUrge = (params) => putAction('/bpm/process-instance/switch-urge', params)

// 工作流：任务
const getTaskListByProcessInstanceId = (params) => getAction('/bpm/task/list-by-process-instance-id', params)
const getTodoTaskPage = (params) => getAction('/bpm/task/todo-page', params)
const getDoneTaskPage = (params) => getAction('/bpm/task/done-page', params)
const checkTodoTask = (params) => getAction('/bpm/task/check-todo', params)
const approveTask = (params) => putAction('/bpm/task/approve', params)
const rejectTask = (params) => putAction('/bpm/task/reject', params)
const updateTaskAssignee = (params) => putAction('/bpm/task/update-assignee', params)
const claimTask = (params) => putAction('/bpm/task/claim', params)

// 工作流：活动
const getActivityList = (params) => getAction('/bpm/activity/list', params)

// 达芬奇
const addDavinciSource = (params) => getAction('/lowcode/fwForms/createDavinciSource', params)

const generateUUID = (params) => getAction('/sys/common/generateUUID', params)

// 文章
const getOperateList = (params) => getAction('/system/sysArticleOperate/list', params)
const getArticleList = (params) => postAction('system/sysArticle/getPages', params)
const getArticleDetail = (params) => getAction('/system/sysArticle/queryById', params)

const getTagTree = (params) => formAction('sg/cdTags/getTagTree', params)

// 吹哨事件
const getDifficultEvent = (params) => postAction('/bpm/process-instance/getDifficultEvent', params)
// 获取设备列表
const getRmResources = (params) => postAction('system/rmResources/gridRmResources', params)

// 发送会商邀请模版消息
const videoWxTemplateSendBatch = (params) => postAction('/sys/wxtemplate/sendBatch', params)

// 设备管理
const getResourceCatsTree = (params) => getAction('/system/rmResourceCats/treelist', params)
const deleteResourcesCat = (params) => deleteAction('/system/rmResourceCats/delete', params)
const deleteBatchResourcesCat = (params) => deleteAction('/system/rmResourceCats/deleteBatch', params)
const addResourceCat = (params) => postAction('/system/rmResourceCats/add', params)
const getResourceCatById = (params) => getAction('/system/rmResourceCats/queryById', params)
const editResourceCat = (params) => putAction('/system/rmResourceCats/edit', params)
const addResource = (params) => postAction('/system/rmResources/add', params)
const editResource = (params) => putAction('/system/rmResources/edit', params)
const getRegionsTree = (params) => postAction('/system/cdRegionalisms/getRegionsTree', params)
const getRegionGrids = (params) => postAction('/system/rmGrid/showGrids', params)

export {
  // imgView,
  // doMian,
  addRole,
  editRole,
  checkRoleCode,
  addUser,
  editUser,
  queryUserRole,
  getUserList,
  queryall,
  frozenBatch,
  checkOnlyUser,
  changePassword,
  getSimpleUserList,
  duplicateUserIdcardCheck,
  getPermissionList,
  getPermissionByObjId,
  getPermissionById,
  addPermission,
  editPermission,
  queryTreeList,
  queryListAsync,
  queryRolePermission,
  saveRolePermission,
  queryPermissionsByUser,
  queryPermissionsByRoleId,
  loadAllRoleIds,
  getPermissionRuleList,
  queryPermissionRule,
  queryDepartTreeList,
  queryDepartTreeSync,
  queryIdTree,
  queryParentName,
  searchByKeywords,
  deleteByDepartId,
  deleteLog,
  deleteLogList,
  addDict,
  editDict,
  treeList,
  addDictItem,
  editDictItem,
  getDictItemAll,
  ajaxGetDictItems,
  doReleaseData,
  doReovkeData,
  getLoginfo,
  getVisitInfo,
  queryUserByDepId,
  duplicateCheck,
  queryPermissionCats,
  queryTreeListForRole,
  getSystemMenuList,
  getSystemSubmenu,
  getSystemSubmenuBatch,
  loadCategoryData,
  loadTreeChildren,
  loadCateTree,
  loadDictItem,
  getRootList,
  getChildList,
  checkRuleByCode,
  queryDepartPermission,
  saveDepartPermission,
  queryTreeListForDeptRole,
  queryDeptRolePermission,
  saveDeptRolePermission,
  getSimpleRoleList,
  queryMyDepartTreeList,
  getUserNoticeInfo,
  getDictItemsFromCache,
  getDictItemsByCode,
  deletePermission,
  inputListAll,
  dealForm,
  formDetail,
  editForm,
  getFormById,
  getFormList,
  downloadForm,
  getFormTitles,
  importFormData,
  exportFormData,
  getFormDataList,
  getDyFormOrderId,
  getAfTables,
  getAfTableColmuns,
  getTableEnumitems,
  subForm,
  deleteAfDatas,
  addAppParams,
  getAppParams,
  saveAppParams,
  deleteAppParams,
  listAppParams,
  getPermissionDetailList,
  addAuth,
  editAuth,
  deleteAuth,
  listAuth,
  getFormAuthByUsername,
  formListAll,
  pageBpmModel,
  getBpmModel,
  createBpmModel,
  updateBpmModel,
  importBpmModel,
  deployBpmModel,
  updateStateBpmModel,
  deleteBpmModel,
  getLastDefinition,
  pageBpmProcessDefinition,
  getBpmProcessDefinition,
  listBpmProcessDefinition,
  getBpmProcessDefinitionXml,
  createTaskAssignRule,
  getTaskAssignRuleList,
  updateTaskAssignRule,
  getUserGroupSimpleList,
  createUserGroup,
  updateUserGroup,
  deleteUserGroup,
  getUserGroup,
  pageUserGroup,
  pageProcessInstance,
  getEventListByUserId,
  pageTodoProcessInstance,
  pageDoneProcessInstance,
  createProcessInstance,
  cancelProcessInstance,
  getProcessInstance,
  switchUrge,
  getOutGoingNames,
  getNodes,
  getTaskListByProcessInstanceId,
  getTodoTaskPage,
  getDoneTaskPage,
  checkTodoTask,
  approveTask,
  rejectTask,
  updateTaskAssignee,
  claimTask,
  getActivityList,
  getDeptRoleList,
  getDepartsByDepartPath,
  getTreeDepartsListByDepartId,
  getDepartsByDepartIdsAndRoles,
  canSeeOuIds,
  dealUserOtherInfo,
  updateAllUserPassword,
  addDavinciSource,
  dealRole2Role,
  getRelRolesByRoleId,
  getFileRowNumber,
  addPage,
  listPage,
  removePageRelation,
  editPage,
  deletePage,
  getPage,
  generateUUID,
  getOperateList,
  getArticleList,
  getArticleDetail,
  getTagTree,
  getDifficultEvent,
  getRmResources,
  getCdDtlAddr,
  getCdDtlAddrDetail,
  getAddressTree,
  videoWxTemplateSendBatch,
  getResourceCatsTree,
  deleteResourcesCat,
  deleteBatchResourcesCat,
  addResourceCat,
  getResourceCatById,
  editResourceCat,
  addResource,
  editResource,
  getRegionsTree,
  getRegionGrids
}
